package com.jwinf.cdLibary.tool;

import com.jwinf.cdLibary.controller.*;
import com.jwinf.cdLibary.domain.CD;
import com.jwinf.cdLibary.domain.User;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Rectangle2D;
import javafx.scene.Cursor;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.stage.Screen;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

import java.awt.*;
import java.io.IOException;
import java.util.Optional;

/**
 * @author jwinf
 * 窗口工具类负责窗口的加载
 */
public class WindowUtils {

    // 改变鼠标样式方法
    // 用具件的公共父类control做参数
    public static void changeMouse(Control control){
        // 添加事件监听器改变鼠标样式
        control.addEventHandler(MouseEvent.MOUSE_ENTERED,event -> control.setCursor(Cursor.HAND));
    }
    public static void MouseWait(Control control){
        control.addEventHandler(MouseEvent.MOUSE_PRESSED, event -> control.setCursor(Cursor.WAIT));
    }

    // 发出系统提示音
    public static void sound(){
        // 系统提示音
        final Runnable runnable = (Runnable) Toolkit.getDefaultToolkit().getDesktopProperty("win.sound.default");
        runnable.run();
    }

    // 添加事件监听器改变图标的大小
    public static void changeIcon(Control control, Label label, String url){


        control.focusedProperty().addListener((observable, oldValue, newValue) -> {
            Image image;
            if (newValue) {
                image = new Image(WindowUtils.class.getResourceAsStream(url), 25, 25, false, false);
            } else {
                image = new Image(WindowUtils.class.getResourceAsStream(url), 16, 16, false, false);
            }
            label.setGraphic(new ImageView(image));
        });
    }

    // 显示指定的窗口 通过加载url显示窗体
    public static void showFrame(String url, String icon, String title) throws IOException {
        FXMLLoader fxmlLoader = FXMLUtils.getFxmlLoader(url);
        Parent root = fxmlLoader.load();
        Stage stage = new Stage(StageStyle.DECORATED);
        stage.setTitle(title);
        stage.getIcons().add(new Image(WindowUtils.class.getResourceAsStream(icon), 128, 128, false, false));
        stage.setScene(new Scene(root, 600, 400));
        stage.setResizable(false);
        stage.show();
    }

    // 显示添加窗体
    public static void showaddFrame(String url, String icon, String title) throws IOException {
        FXMLLoader fxmlLoader = FXMLUtils.getFxmlLoader(url);
        Parent root = fxmlLoader.load();
        Stage stage = new Stage(StageStyle.DECORATED);
        stage.setTitle(title);
        stage.getIcons().add(new Image(WindowUtils.class.getResourceAsStream(icon), 128, 128, false, false));
        stage.setScene(new Scene(root, 500, 400));
        stage.setResizable(false);
        stage.show();
    }

    // 显示添加cd窗体
    public static void showaddcdFrame() throws IOException {
        FXMLLoader fxmlLoader = FXMLUtils.getFxmlLoader("/com/jwinf/cdLibary/view/AddCdFrame.fxml");
        Parent root = fxmlLoader.load();
        Stage stage = new Stage(StageStyle.DECORATED);
        stage.setTitle("添加光盘");
        stage.getIcons().add(new Image(WindowUtils.class.getResourceAsStream("/icon/loginIcon.png"), 128, 128, false, false));
        stage.setScene(new Scene(root, 500, 350));
        stage.setResizable(false);
        stage.show();
    }

    // 显示编辑用户窗体
    public static void showeditFrame(User user) throws IOException {
        FXMLLoader fxmlLoader = FXMLUtils.getFxmlLoader("/com/jwinf/cdLibary/view/EditUserFrame.fxml");
        Parent root = fxmlLoader.load();
        Stage stage = new Stage(StageStyle.DECORATED);
        EditUserFrameController controller = fxmlLoader.getController();
        controller.init(user);
        stage.setTitle("编辑用户信息");
        stage.getIcons().add(new Image(WindowUtils.class.getResourceAsStream("/icon/loginIcon.png"), 128, 128, false, false));
        stage.setScene(new Scene(root, 500, 400));
        stage.setResizable(false);
        stage.show();
    }

    // 通过多态 实现显示编辑cd窗体
    public static void showeditFrame(CD cd) throws IOException {
        FXMLLoader fxmlLoader = FXMLUtils.getFxmlLoader("/com/jwinf/cdLibary/view/EditCdFrame.fxml");
        Parent root = fxmlLoader.load();
        Stage stage = new Stage(StageStyle.DECORATED);
        EditCdFrameController controller = fxmlLoader.getController();
        controller.init(cd);
        stage.setTitle("编辑用户信息");
        stage.getIcons().add(new Image(WindowUtils.class.getResourceAsStream("/icon/loginIcon.png"), 128, 128, false, false));
        stage.setScene(new Scene(root, 500, 350));
        stage.setResizable(false);
        stage.show();
    }

    // 显示窗体 通过该窗体需要初始化设置user对象
    public static void showFrame(String url, String icon, String title, User user) throws IOException {
        FXMLLoader fxmlLoader = FXMLUtils.getFxmlLoader(url);
        Parent root = fxmlLoader.load();
        Stage stage = new Stage(StageStyle.DECORATED);
        CdController controller = fxmlLoader.getController();
        controller.setUser(user);

        stage.setTitle(title);
        stage.getIcons().add(new Image(WindowUtils.class.getResourceAsStream(icon), 128, 128, false, false));
        stage.setScene(new Scene(root, 600, 400));
        stage.setResizable(false);
        stage.show();
    }

    // 显示归还窗体
    public static void showReturnFrame(User user) throws IOException {
        FXMLLoader fxmlLoader = FXMLUtils.getFxmlLoader("/com/jwinf/cdLibary/view/ReturnFrame.fxml");
        Parent root = fxmlLoader.load();
        Stage stage = new Stage(StageStyle.DECORATED);
        ReturnController controller = fxmlLoader.getController();
        controller.setUser(user);
        controller.init();

        stage.setTitle("归还");
        stage.getIcons().add(new Image(WindowUtils.class.getResourceAsStream("/icon/loginIcon.png"), 128, 128, false, false));
        stage.setScene(new Scene(root, 600, 400));
        stage.setResizable(false);
        stage.show();
    }

    // 弹出确认对话框 返回用户的选择
    public static Optional<ButtonType> inquiryAlert(String message, String style){
        // 定义一个弹窗
        Alert alert = new Alert(Alert.AlertType.CONFIRMATION, message);

        // 获得运行窗体
        Rectangle2D bounds = Screen.getPrimary().getVisualBounds();
        alert.setX(420);
        alert.setY(bounds.getMinY() + bounds.getHeight() / 3);
        alert.setWidth(200);

        // 设置弹窗头部为空
        alert.setHeaderText(null);

        // 设置图标
        Image image = new Image("/icon/warnIcon.png");
        Stage stage = (Stage)alert.getDialogPane().getScene().getWindow();
        stage.getIcons().add(image);
        alert.setGraphic(new ImageView(image));
        alert.getDialogPane().setStyle(style);


        // 根据自定义工具类发出系统提示音
        WindowUtils.sound();

        // 显示窗体
       return alert.showAndWait();
    }

    // 弹出提示对话框
    public static void showTip(String message, String icon, String style){

        // 定义一个弹窗
        Alert alert = new Alert(Alert.AlertType.INFORMATION, message);

        // 获得运行窗体
        Rectangle2D bounds = Screen.getPrimary().getVisualBounds();
        alert.setX(420);
        alert.setY(bounds.getMinY() + bounds.getHeight() / 3);
        alert.setWidth(200);

        // 设置弹窗头部为空
        alert.setHeaderText(null);

        // 设置图标
        Image image = new Image(icon);
        Stage stage = (Stage)alert.getDialogPane().getScene().getWindow();
        stage.getIcons().add(image);
        alert.setGraphic(new ImageView(image));
        alert.getDialogPane().setStyle(style);


        // 根据自定义工具类发出系统提示音
        WindowUtils.sound();

        // 显示窗体
        alert.showAndWait();
    }

    // 销毁组件所在的窗体
    public static void destroyStage(Control control){
         Stage stage = (Stage)control.getScene().getWindow();
         stage.close();
    }
}
